package com.zh.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.getui.push.v2.sdk.api.UserApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.CidAliasListDTO;
import com.zh.service.IGeTuiService;
import com.zh.template.GetTuiTemplate;
import com.zh.utils.AssertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class GeTuiServiceImpl implements IGeTuiService {
    @Autowired
    private GetTuiTemplate getTuiTemplate;

    /**
     * 绑定个推别名
     *
     * @param cid
     */
    @Override
    public void bind(String cid) {
        try {
            // 创建UserApi
            UserApi userApi = getTuiTemplate.createUserApi();
            CidAliasListDTO cidAliasListDTO = new CidAliasListDTO();
            // 绑定别名， 这里我们采用将自己的登录ID进行绑定
            String alias = StpUtil.getLoginIdAsString();
            cidAliasListDTO.add(new CidAliasListDTO.CidAlias(cid, alias));
            ApiResult<Void> apiResult = userApi.bindAlias(cidAliasListDTO);
            log.info("个推绑定别名结果：{}", apiResult);
            AssertUtil.isEquals(apiResult.getCode(), 0, "个推绑定别名失败");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}
